import 'package:development_assistant/config/font.dart';
import 'package:development_assistant/controller/database_controller.dart';
import 'package:development_assistant/model/column_info.dart';
import 'package:development_assistant/model/table_info.dart';
import 'package:development_assistant/ui/screen/widget/custom_text.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

class TableDetail extends StatelessWidget {
  TableDetail({super.key});

  final DatabaseController databaseController=Get.put(DatabaseController());

  @override
  Widget build(BuildContext context) {

    return Scaffold(
      appBar: _appBar(),
      bottomNavigationBar: _updateTable(),
      floatingActionButton: _deleteTable(),
      body: SingleChildScrollView(
        child: Container(
          padding: EdgeInsets.symmetric(vertical: 20.w,horizontal: 20.w),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              CustomText(text: "数据表".tr, textStyle: FontConfig.titleStyle),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  _tableField("数据表名".tr,databaseController.tableName),
                  _tableField("排序方法".tr, databaseController.tableSort)
                ],
              ),
              _tableField("引擎".tr,databaseController.tableEngine),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  CustomText(text: "Column", textStyle: FontConfig.titleStyle),
                  IconButton(
                    splashColor: Colors.transparent,
                    highlightColor: Colors.transparent,
                    onPressed: (){
                      ColumnInfo column=ColumnInfo(null, databaseController.tableID.value, "", "", "");
                      databaseController.columns.add(column);
                    },
                    icon: const Icon(Icons.add),
                  ),
                ],
              ),
              _tableColumn()
            ],
          ),
        ),
      ),
    );
  }

  _updateTable(){
    return Container(
      width: 200.w,
      height: 35.h,
      color: Colors.white,
      margin: EdgeInsets.symmetric(vertical: 10.h),
      padding: EdgeInsets.symmetric(horizontal: 100.w),
      child: ElevatedButton(
        onPressed: (){
          Get.defaultDialog(
            title: "修改数据表".tr,
            middleText: "${'是否修改该数据表?'.tr} : ${databaseController.tableName}",
            confirm: TextButton(
              style: TextButton.styleFrom(
                  backgroundColor: const Color(0xffC77D7D),
                  foregroundColor: Colors.white,
                  shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(10)
                  )
              ),
              onPressed: (){
                databaseController.updateTable();
              },
              child:Text("确定".tr),
            ),
            cancel:TextButton(
              style: TextButton.styleFrom(
                  backgroundColor: const Color(0xffC77D7D),
                  foregroundColor: Colors.white,
                  shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(10)
                  )
              ),
              onPressed: (){
                Get.back();
              },
              child: Text("取消".tr),
            ),
          );
        },
        style: ElevatedButton.styleFrom(
            backgroundColor: const Color(0xffC77D7D),
            foregroundColor: Colors.white,
            shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(20)
            )
        ),
        child:  Text("修改数据表".tr,style: TextStyle(fontWeight: FontWeight.bold),),
      ),
    );
  }

  _deleteTable(){
    return Container(
      width: 50.w,
      height: 50.w,
      decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(50),
          gradient: const LinearGradient(
              colors: [
                Color(0xFFfeae8b),
                Color(0xFFf7e4ce),
              ],
              begin: Alignment.topCenter,
              end: Alignment.bottomCenter
          )
      ),
      child: Center(
        child: IconButton(
          onPressed: (){
            Get.defaultDialog(
              title: "删除数据表".tr,
              middleText: "${'是否删除数据表?'.tr} : ${databaseController.tableName}",
              confirm: TextButton(
                style: TextButton.styleFrom(
                    backgroundColor: const Color(0xffC77D7D),
                    foregroundColor: Colors.white,
                    shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(10)
                    )
                ),
                onPressed: (){
                  databaseController.delTable();
                },
                child:Text("确定".tr),
              ),
              cancel:TextButton(
                style: TextButton.styleFrom(
                    backgroundColor: const Color(0xffC77D7D),
                    foregroundColor: Colors.white,
                    shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(10)
                    )
                ),
                onPressed: (){
                  Get.back();
                },
                child: Text("取消".tr),
              ),
            );
          },
          icon: const Icon(Icons.delete),
          color: Colors.black26,
        ),
      ),
    );
  }

  _tableColumn(){
    return Obx(() => Wrap(
      children: List<Widget>.generate(databaseController.columns.length, (index) {
        return Container(
          margin: EdgeInsets.only(bottom: 10.h),
          decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(10),
              border: Border.all(color:Colors.grey,width: 1)
          ),
          child: Row(
            children: [
              SizedBox(
                width: 80.w,
                child: TextField(
                  controller: TextEditingController(text: databaseController.columns[index].columnName),
                  decoration: const InputDecoration(
                      hintText: "Column",
                      border: OutlineInputBorder(
                          borderSide: BorderSide.none
                      )
                  ),
                  onChanged: (e){
                    databaseController.columns[index].columnName=e;
                  },
                ),
              ),
              SizedBox(
                width: 80.w,
                child: TextField(
                  controller: TextEditingController(text: databaseController.columns[index].columnType),
                  decoration: const InputDecoration(
                      hintText: "Type",
                      border: OutlineInputBorder(
                          borderSide: BorderSide.none
                      )
                  ),
                  onChanged: (e){
                    databaseController.columns[index].columnType=e;
                  },
                ),
              ),
              SizedBox(
                width: 80.w,
                child: TextField(
                  controller: TextEditingController(text: databaseController.columns[index].columnKind),
                  decoration: const InputDecoration(
                      hintText: "Kind",
                      border: OutlineInputBorder(
                          borderSide: BorderSide.none
                      )
                  ),
                  onChanged: (e){
                    databaseController.columns[index].columnKind=e;
                  },
                ),
              ),
              IconButton(
                  onPressed: (){
                    databaseController.columns.removeAt(index);
                  },
                  icon: Icon(Icons.delete_outline)
              )
            ],
          ),
        );
      })));
  }

  _tableField(String hintText,RxString tableValue){
    return Container(
      width: hintText!="引擎"&&hintText!="Engine"?155.w:320.w,
      height: 50.h,
      margin: EdgeInsets.symmetric(vertical: 5.h),
      child: Center(
        child: TextField(
          controller: TextEditingController(text: tableValue.value),
          maxLines: 1,
          decoration: InputDecoration(
              hintText:hintText,
              border: OutlineInputBorder(
                borderRadius: BorderRadius.circular(15),
              )
          ),
          onChanged: (e){
            tableValue.value=e;
          },
        ),
      ),
    );
  }

  _appBar(){
    return AppBar(
      title: Text("数据表详情".tr),
      centerTitle: true,
      leading: IconButton(
        onPressed: (){
          databaseController.columns.clear();
          Get.offNamed("/Home",arguments: {"selectIndex":3});
        },
        icon: const Icon(Icons.arrow_back),
      ),
    );
  }

}
